package com.stylefeng.guns.modular.transcapacitymag.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.service.IOrderInfService;
import com.stylefeng.guns.modular.system.service.ISysSerialnumInfService;
import com.stylefeng.guns.modular.transcapacitymag.dao.PreOrderInfMapper;
import com.stylefeng.guns.modular.transcapacitymag.model.PreOrderInf;
import com.stylefeng.guns.modular.transcapacitymag.service.IPreOrderInfService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.toolkit.IdWorker;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;

/**
 * 预下单信息 Service 实现
 *
 * @author xujianjie
 * @Date 2019-03-05 13:50
 */
@Service
public class PreOrderInfServiceImpl extends ServiceImpl<PreOrderInfMapper, PreOrderInf> implements IPreOrderInfService
{
    @Resource
    private PreOrderInfMapper preOrderInfMapper;

    @Autowired
    private ISysSerialnumInfService sysSerialnumInfService;

    @Autowired
    private IOrderInfService orderInfService;

    /**
     * 删除预下单信息
     *
     * @param preOrderInf
     * @return
     */
    @Transactional
    public int deletePreOrderInf(PreOrderInf preOrderInf, String token) throws Exception
    {
        if (preOrderInf == null)
        {
            throw new RuntimeException("参数不全！");
        }
        return this.preOrderInfMapper.deletePreOrderInf(preOrderInf);
    }

    /**
     * 批量删除预下单信息
     *
     * @param ids 主键ID数组
     * @return
     */
    @Transactional
    public int deleteBatchPreOrderInf(String[] ids, String token) throws Exception
    {
        return this.preOrderInfMapper.deleteBatchPreOrderInf(ids);
    }

    /**
     * 查询预下单信息列表
     *
     * @param preOrderInf 预下单信息对象
     * @return
     */
    public List<PreOrderInf> selectListPreOrderInf(PreOrderInf preOrderInf, String token) throws Exception
    {
        if (preOrderInf != null && preOrderInf.getDispather() != null && preOrderInf.getDispather().equals("1"))
        {
            preOrderInf.setDispatherLineIds(orderInfService.getDispatherLineIds(token));
        }
        return this.preOrderInfMapper.selectListPreOrderInf(preOrderInf);
    }

    /**
     * 分页查询预下单信息列表
     *
     * @param pager 分页对象
     * @return
     */
    public Map pageListPreOrderInf(Pager pager, String token) throws Exception
    {
        Page page = new Page(pager.getPageNumber(), pager.getPageSize());

        PreOrderInf preOrderInf = (PreOrderInf) pager.getObj();
        if (preOrderInf != null && preOrderInf.getDispather() != null && preOrderInf.getDispather().equals("1"))
        {
            preOrderInf.setDispatherLineIds(orderInfService.getDispatherLineIds(token));
        }

        page.setRecords(preOrderInfMapper.selectListPreOrderInf(page, preOrderInf));
        List<PreOrderInf> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }

    /**
     * 同一个事务 互斥锁查询
     *
     * @param preOrderInf 预下单信息对象
     * @return
     */
    public List<PreOrderInf> selectListPreOrderInfForUpdate(PreOrderInf preOrderInf) throws Exception
    {
        return this.preOrderInfMapper.selectListPreOrderInfForUpdate(preOrderInf);
    }

    /**
     * 通过主键id查询的预下单信息列表
     *
     * @param id 主键id
     * @return
     */
    public PreOrderInf findPreOrderInfById(String id, String token) throws Exception
    {
        return this.preOrderInfMapper.findPreOrderInfById(id);
    }

    /**
     * 通过主键id查询锁的预下单信息列表
     *
     * @param id 主键id
     * @return
     */
    public PreOrderInf findPreOrderInfByIdForUpdate(String id, String token) throws Exception
    {
        return this.preOrderInfMapper.findPreOrderInfByIdForUpdate(id);
    }

    /**
     * 新增预下单信息数据
     *
     * @param preOrderInf 预下单信息对象
     * @return
     */
    @Transactional
    public int insertPreOrderInf(PreOrderInf preOrderInf, String token) throws Exception
    {
        if (preOrderInf != null && (preOrderInf.getOiId() == null || preOrderInf.getOiId().trim().length() < 10))
        {
            preOrderInf.setOiId(IdWorker.get32UUID());
            String dateFormatStr = StringUtils.remove(DateUtil.today(), "-");
            preOrderInf.setOiCode(sysSerialnumInfService.updateSysSerialnumInf(preOrderInf.getOtId() + "O", dateFormatStr, 6, null));
            preOrderInf.setPayState("0");
            preOrderInf.setOiQrcode(String.valueOf((int) ((Math.random() * 9 + 1) * 100000)));
            Date date = new Date();
            preOrderInf.setCreateTime(date);
            preOrderInf.setCiValidTime(new Date(date.getTime() + 1000 * 60 + 30));
            preOrderInf.setConfirmState("0");
        }

        return this.preOrderInfMapper.insertPreOrderInf(preOrderInf);
    }

    /**
     * 更新预下单信息数据
     *
     * @param preOrderInf 预下单信息对象
     * @return
     */
    @Transactional
    public int updatePreOrderInf(PreOrderInf preOrderInf, String token) throws Exception
    {
        return this.preOrderInfMapper.updatePreOrderInf(preOrderInf);
    }

    /**
     * 查询预下单信息返回key-Value属性
     *
     * @param preOrderInf 系统操作对象
     * @return
     */
    public List<CommonKeyValue> optionsPreOrderInf(PreOrderInf preOrderInf, String token) throws Exception
    {
        if (preOrderInf != null && preOrderInf.getDispather() != null && preOrderInf.getDispather().equals("1"))
        {
            preOrderInf.setDispatherLineIds(orderInfService.getDispatherLineIds(token));
        }

        List<PreOrderInf> listPreOrderInf = this.preOrderInfMapper.selectListPreOrderInf(preOrderInf);
        List<CommonKeyValue> listOptions = new ArrayList<CommonKeyValue>();
        for (PreOrderInf item : listPreOrderInf)
        {
            CommonKeyValue opts = new CommonKeyValue();
            opts.setId("" + item.getOiId());
            opts.setValue(item.getOlName());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
